const os = require('os');
const logger = require('./logger'); // 导入logger.js中的logger对象
const { exec } = require('child_process');
let { ipcRenderer, shell } = require('electron');

function selectAllScenario(checkBox) {
	var checkboxes = document.querySelectorAll('.scenarioSelection input[type="checkbox"]');
	for (var i = 0; i < checkboxes.length; i++) {
		checkboxes[i].checked = checkBox.checked;
	}
}

let configData = [];

function showDependences(arg) {
	configData = [];
	configData.push(arg);
	var elements = document.getElementsByClassName("scenarioSelection");
	let configList = {};
	let formattedDoc = '';

	let selectAllVal = document.getElementById("selectAll");
	var checks = document.querySelectorAll(".scenarioSelection > input");
	selectAllVal.onclick = function () {
		// 遍历复选框元素对象
		for (var i = 0; i < checks.length; i++) {
			// 将复选框的选中状态设置为和全选框一致
			if (checks[i].value != "JavaScript_key")
				checks[i].checked = selectAllVal.checked;
		}
	};

	for (var i = 0; i < elements.length; i++) {
		elements[i].addEventListener("change", function (event) {
			// 根据选中不同的checkbox，显示不同输出；
			if (event.target.checked) {
				var selectedValue = event.target.value;
				var firstClassList = configData[0]["first_class_list"];
				if (selectedValue === "selectAll") {
					let firstClassList = configData[0]["first_class_list"];
					for (let key in firstClassList) {
						if (key.endsWith("_key") && key != "JavaScript_key") {
							configList[key] = configData[0]["second_class"][key];
						}
					}
				} else {
					configList[selectedValue] = configData[0]["second_class"][selectedValue];
				}
			} else {
				var selectedValue = event.target.value;
				if (selectedValue === "selectAll") {
					configList = {};
				} else {
					delete configList[selectedValue];
				}
			}

			//页面更新
			let extList = [];
			let depList = [];
			let scriptList = [];
			if (Object.keys(configList).length > 0) {
				document.getElementById("nextStep").disabled = false;
			} else {
				document.getElementById("nextStep").disabled = true;
			}

			for (let key in configList) {
				for (let val of configList[key]["ext-list"]) {
					extList.push(val);
				}
			}
			extList = [...new Set(extList)];

			for (let key in configList) {
				for (let val of configList[key]["script-list"]) {
					scriptList.push(val);
				}
			}

			for (let key in configList) {
				let tDepList = configList[key]["pkg-list"];
				//暂时只做deb包的处理
				// if (tDepList["rpm"]) {
				// 	for (let val of tDepList["rpm"]) {
				// 		depList.push(key);
				// 	}
				// }
				// else {
				// 	for (let key in tDepList["deb"]) {
				// 		depList.push(key);
				// 	}
				// }
				if (tDepList["deb"]) {
					for (let key in tDepList["deb"]) {
						depList.push(key);
					}
				}
			}
			depList = [...new Set(depList)];


			var SelectedDatas = {
				configList: configList,
				extList: extList,
				depList: depList,
				scriptList: scriptList
			}

			ipcRenderer.send('message-to-jsb', SelectedDatas);
			if (depList.length > 0) {
				formattedDoc = '<h2>软件列表：<span style="color: yellow; font-size: 12px;">请点击下一步输入密码安装以下软件</span></h2>';
			}
			else
				formattedDoc = '<h2>软件列表：</h2>';

			formattedDoc += Object.values(depList).map(element => `<h4 style='font-size: 16px; line-height: 0.1;margin-left:20px;'>${element}</h4>`).join('');

			formattedDoc += '<h2>插件列表：</h2>';
			formattedDoc += extList.map(element => `<h4 style='font-size: 16px; line-height: 0.1;margin-left:20px;'>${element}</h4>`).join('');

			document.getElementById("outputPre").innerHTML = formattedDoc;
		});
	}
}

window.onload = function () {
	configData = [];
}

function getSysInfo() {
	logger.info(os.arch());
	// document.getElementById('platformInfo').innerText = `CPU架构： ${cpuArchitecture}`;
	const platformInfoInput = document.getElementById("platformInfo");
	platformInfoInput.value = os.arch();

	// 执行 uname -m 命令
	exec('uname -m', (error, stdout, stderr) => {
		if (error) {
			logger.error('执行命令时出错:', error);
		}
		// 输出命令的标准输出
		const architecture = stdout.trim();
		platformInfoInput.value = architecture
	});

	const osInfoInput = document.getElementById("osInfo");
	exec('lsb_release -a', (error, stdout, stderr) => {
		let osInfo = "";
		if (error) {
			logger.error("get osinfo fail: lsb_release -a");
		}
		const descriptionMatch = stdout.match(/Description:\s*(.*)/);
		if (descriptionMatch && descriptionMatch[1]) {
			osInfo = descriptionMatch[1].trim();
			console.log('Description:', osInfo);
		} else {
			console.log('未找到 Description 信息');
		}

		console.log("osinfo: " + osInfo);
		exec('uname -a', (error, stdout, stderr) => {
			if (error) {
				console.error('执行第二个命令时出错:', error);
				logger.error("get osinfo fail: uname -a");
			}

			// 使用正则表达式匹配年份信息
			const yearMatch = stdout.match(/\b20\d{2}\b/);

			if (yearMatch) {
				const year = yearMatch[0];
				osInfo = osInfo + " " + year;
				osInfoInput.value = osInfo;
				// 请确保 osInfoInput 的定义
				console.log("osinfo: " + osInfo);
				logger.info("osInfo: " + osInfo);
			} else {
				console.log('未找到年份信息');
			}
		});

	});

}
// getSysInfo()
